<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动类型" prop="actType">
        <el-select v-model="queryParams.actType" placeholder="请选择农事活动类型" clearable>
          <el-option
            v-for="dict in dict.type.argo_activity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:info:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="农事活动代号" align="center" prop="id" />-->
      <el-table-column label="农事活动类型" align="center" prop="actType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.argo_activity_type" :value="scope.row.actType"/>
        </template>
      </el-table-column>
      <el-table-column label="描述信息" align="center" prop="actDepict" />
      <el-table-column label="投入品" align="center" prop="inputsName" />
      <el-table-column label="投入品数量" align="center" prop="inputsNum" />
      <el-table-column label="农作物" align="center" prop="cropsName" />
      <el-table-column label="农产品" align="center" prop="productDepic" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农事活动信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动类型" prop="actType">
          <el-select v-model="form.actType" placeholder="请选择农事活动类型">
            <el-option
              v-for="dict in dict.type.argo_activity_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述信息" prop="actDepict">
          <el-input v-model="form.actDepict" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="投入品" prop="inputsId">
          <el-select v-model="form.inputsId" placeholder="请选择" clearable>
            <el-option
              v-for="item in inputsList"
              :key="item.id"
              :label="item.inputsName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="inputsNum">
          <el-input v-model="form.inputsNum" placeholder="请输入投入品数量" />
        </el-form-item>
        <el-form-item label="农作物" prop="cropsId">
          <el-select v-model="form.cropsId" placeholder="请选择" clearable>
            <el-option
              v-for="item in cropsList"
              :key="item.id"
              :label="item.cropsName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="农产品" prop="productId">
          <el-select v-model="form.productId" placeholder="请选择" clearable>
            <el-option
              v-for="item in productsList"
              :key="item.id"
              :label="item.productDepic"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/activity/info";
import request from '@/utils/request'

// 查询投入品信息列表
function listInputs(query) {
  return request({
    url: '/inputs/info/list',
    method: 'get',
    params: query
  })
}
// 查询农作物(播种后)列表
function listCrops(query) {
  return request({
    url: '/crops/info/list',
    method: 'get',
    params: query
  })
}
// 查询农产品信息列表
function listProducts(query) {
  return request({
    url: '/product/info/list',
    method: 'get',
    params: query
  })
}

export default {
  name: "Info",
  dicts: ['argo_activity_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农事活动信息表格数据
      infoList: [],
      // 投入品信息表格数据
      inputsList: [],
      cropsList: [],
      productsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actType: null,
        inputsName: null,
        cropsName: null,
        productDepic: null,
      },
      // 查询参数
      queryCropParams: {
        pageNum: 1,
        pageSize: 100,
        cropsName: null,
        cropsType: null,
        createTime: null,
        pickedTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getInputsList();
    this.getCropsList();
    this.getProductsList();
  },
  methods: {
    /** 查询农事活动信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询投入品信息列表 */
    getInputsList() {
      this.loading = true;
      listInputs({
        pageNum: 1,
        pageSize: 100,
        id: null,
        inputsName: null,
      }).then(response => {
        this.inputsList = response.rows;
        this.loading = false;
      });
    },
    /** 查询农作物(播种后)列表 */
    getCropsList() {
      this.loading = true;
      this.queryCropParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryCropParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryCropParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangePickedTime && '' != this.daterangePickedTime) {
        this.queryCropParams.params["beginPickedTime"] = this.daterangePickedTime[0];
        this.queryCropParams.params["endPickedTime"] = this.daterangePickedTime[1];
      }
      listCrops(this.queryCropParams).then(response => {
        this.cropsList = response.rows;
        this.loading = false;
      });
    },
    /** 查询农产品信息列表 */
    getProductsList() {
      this.loading = true;
      listProducts({
        pageNum: 1,
        pageSize: 100,
        productType: null,
        productDepic: null,
      }).then(response => {
        this.productsList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        actType: null,
        actDepict: null,
        userId: null,
        createTime: null,
        inputsId: null,
        inputsNum: null,
        cropsId: null,
        productId: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加农事活动信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改农事活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除农事活动信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
